<template>
  <el-drawer
    title="选择文案"
    :visible.sync="creativeTextSelection"
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    :wrapperClosable="false"
    :modal="false"
    @close="handleTextSelectionClose"
    size="60%"
  >
    <el-divider></el-divider>
    <div>
      <!--                       @input="searchCreativeText"-->
      <el-row style="margin-left: 10px;margin-right: 10px">
        <el-col :span="18">
          <el-input v-model="textSearchData" style="width:100%"
                    @input="searchCreativeText"
                    placeholder="请输入要搜索的内容"></el-input>
        </el-col>
      </el-row>
      <div class="drawer__content">
        <el-table
          ref="formalDocs"
          row-key="id"
          :data="filterTextData"
        >
          <el-table-column label="广告文案" prop="text" width="200"/>
          <el-table-column label="语言" prop="language"/>
          <el-table-column label="投放媒体" prop="platform"/>
          <el-table-column label="解释" prop="description" width="200"/>
          <el-table-column label="操作">
            <template slot-scope="{row}">
              <el-button type="primary" size="mini" @click="selectDocs(row)">更换</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="bottom-page clearfix">
          <el-pagination
            @current-change="handleTextCurrentChange"
            :current-page="textPage.current"
            :page-sizes="[textPage.pageSize]"
            :page-size="textPage.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="searchTextData.length">
          </el-pagination>
        </div>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import {getTextList} from "@/api/campaign-management/campaign-management";

  export default {
    name: "DocsSingleSelectDrawer",
    props:{
      visibleDocs: {
        type:Boolean,
        default: false
      },
      offerId:String
    },
    data(){
      return {
        textData: [],
        textSearchData: '',
        searchTextData: [],
        creativeTexts: [],
        textPage: {
          pageSize: 8,
          current: 1,
          total: 0
        },
      }
    },
    computed: {
      creativeTextSelection: {
        get() {
          return this.visibleDocs
        },
        set(val) {
          this.$emit('update:visibleDocs', val || false)
        }
      },
      filterTextData() {
        return this.searchTextData.slice((this.textPage.current - 1) * this.textPage.pageSize,
          this.textPage.current * this.textPage.pageSize)
      }
    },
    created() {
      this.getTextData()
    },
    methods:{
      selectDocs(row){
        this.$emit("updateFormalAdText", row.text)
        this.handleTextSelectionClose()
      },
      handleTextSelectionClose(){
        this.creativeTextSelection = false
        this.creativeTexts = []
      },
      searchCreativeText(val) {
        if (this.textSearchData.length === 0)
          this.searchTextData = this.textData
        else {
          this.searchTextData = []
          this.textData.forEach(item => {
            if (item.text.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.platform.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.description.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.language.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
          })
          this.textPage.current = 1
        }
      },
      handleTextCurrentChange(val){
        this.textPage.current = val
      },
      getTextData(){
        getTextList(this.offerId).then(res => {
          this.searchTextData = this.textData = res.data.data;
          this.textData.forEach(item => {
            this.creativeTexts.push(item.text)
          })
        })
      },
    }
  }
</script>
